package cn.com.pansky.sgss.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import net.vsame.url2sql.action.CommonAction;
import net.vsame.url2sql.helper.SqlHelper;
import net.vsame.url2sql.sql.Model;
import net.vsame.url2sql.utils.PostParam;
import cn.com.pansky.action.imgContrast.ImgContrast;
import cn.com.pansky.action.logs.Log;
import cn.com.pansky.helper.PostHelper;
import cn.com.pansky.service.CommonService;
import cn.com.pansky.util.LssConstant;

import com.alibaba.fastjson.JSONObject;

/**
 * 我要认证
 * 
 * @author nobody
 *
 */
public class AuthAction extends CommonAction {
	private Log log = new Log();
	private CommonService cs = new CommonService();


	public void authentication() {
		String CA001 = getParam("CA001");
		//日志记录start
		String AF001 = log.start(CA001, "RZ");
		// 获取刷脸图片
		String image_best = getParam("image_best");
		String image_env = getParam("image_env");
		String delta = getParam("delta");
		Properties p = new Properties();
		try {
			p.load(getClass().getClassLoader().getResourceAsStream(
					"config.properties"));
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		String filePath = p.getProperty("imgPath");
		// 指定身份证照片保存路径：
		String filename = filePath + CA001 + ".jpg";
		// 拿到实名认证身份证照片：
		cs.getIdcardPic(CA001, filename);

		// 调用照片比对方法，比对刷脸与身份证照
		JSONObject result = ImgContrast.imgCompare(filename, image_best, image_env, delta );
		if (0 != result.getIntValue("code")) {
			datas.put("isPass", false);
			datas.put("info", "请重新上传身份证照片");
		}
		Double confidence = result.getDoubleValue("confidence");
		System.out.println("认证结果："+confidence);

		List<PostParam> param = new ArrayList<PostParam>();
		PostParam AAC002 = new PostParam("AAC002", "string", getCA006ByCA001());// 身份证号
		PostParam AAC003 = new PostParam("AAC003", "string", getCA003ByCA001());// 姓名
		PostParam BIC110 = new PostParam("BIC110", "string",
				String.valueOf(confidence));// 相似度
		InputStream in = null;
		try {
			in = new FileInputStream(image_best);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		param.add(new PostParam("img", null, "17.sdc", in));
		param.add(AAC002);
		param.add(AAC003);
		param.add(BIC110);
		PostParam BIC101 = null;
		try {
			if (confidence > LssConstant.CONFIDENCE) {
				// 更新本地特征
				String findRC06 = urlConfig.getVar("findRC06");
				Model m = SqlHelper.queryOne(findRC06, CA001);
				// 判断库中是否有这个人的特征信息：
				String id = "";
				if (m != null) {
					id = m.getString("CA011");
				}
				cs.isnertRC06(image_best, CA001, confidence, id);

				BIC101 = new PostParam("BIC101", "string", "1");// 认证成功
				datas.put("isPass", true);
			} else {
				BIC101 = new PostParam("BIC101", "string", "0");// 认证失败
				datas.put("isPass", false);
			}
			param.add(BIC101);
			PostHelper.send("auth/add.json", param);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {

			// 将身份证临时照片删除
			File file = new File(filename);
			file.delete();
			// 将我要认证刷脸图片删除
			File file2 = new File(image_best);
			file2.delete();
			File file3 = new File(image_env);
			file3.delete();
		}
		
		//日志记录end
		boolean result1 = log.end(AF001, datas.get("isPass"), " ");
		System.out.println("日志：认证"+result1);
		
		context.getDatas().put("result", datas);

	}
}
